//index.js
import api from "../../utils/api.js";
import idcard from '../../utils/idcard.js'
import Toast from '@vant/weapp/toast/toast';
const app = getApp();

Page({
  data: {
    info: {
      photos: [],
      realName: null,
      sex: null,
      identityNumber: null,
      photo: null,
      account: null,
      password: null,
      nextPassWord: null,
      address: null,
      cardNumber: null,
    },
    columns: [
      { text: '男', value: '1' },
      { text: '女', value: '0' },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({ value: null })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  setShowPicker: function (e) {
    const showPicker = e.currentTarget.dataset.flag == '1' ? true : false
    this.setData({ showPicker });
  },

  onConfirmPick: function (e) {
    let info = this.data.info
    info.sex = e.detail.value.text
    this.setData({ showPicker: false, info });
  },
  verifyId: function (e) {
    if (!idcard.checkIdCard(e.detail.value)) {
      Toast({
        message: "请填写正确的身份证号",
        duration: 1500
      });
      this.setData({ error: true })
    } else {
      this.setData({ error: false })
    }
  },

  setValue: function (event) {
    let info = this.data.info
    const type = event.currentTarget.dataset.name
    info[type] = event.detail
    this.setData({ info })
  },

  changeCountError: function () {
    this.setData({ countError: false })
  },

  onSubmit: function (event) {
    const info = this.data.info
    if (this.if_obj_is_null(info)) {
      return Toast({
        message: "请填写完整会员信息",
        duration: 1500
      });
    } else if (info.password != info.nextPassWord) {
      return Toast({
        message: "两次密码不相同，请重新输入",
        duration: 1500
      });
    } else if (this.data.error) {
      return Toast({
        message: "请填写正确的身份证号",
        duration: 1500
      });
    } else if (this.data.countError) {
      return Toast({
        message: "登录账号重复，请重新输入",
        duration: 1500
      });
    }
    const value = {
      photo: this.data.info.photo,
      sex: this.data.info.sex == '男' ? '1' : '0',
      realName: this.data.info.realName,
      identityNumber: this.data.info.identityNumber,
      phone: this.data.info.phone,
      cardNumber: this.data.info.cardNumber,
      account: this.data.info.account,
      password: this.data.info.password,
      address: this.data.info.address
    }
    const _that = this
    wx.request({
      url: api.registermMember,
      method: "POST",
      data: value,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        //更改备注成功回调函数
        if (res.data.code == 200) {
          Toast({
            message: "注册成功",  
            duration: 1500
          })
          _that.setData({
            info: {
              photos: [],
              realName: null,
              sex: null,
              identityNumber: null,
              photo: null,
              account: null,
              password: null,
              nextPassWord: null,
              address: null,
              cardNumber: null,
            }
          })
        }else{
          Toast({
            message: res.data.message || "注册失败",
            duration: 1500
          })
        }
      },
      fail(res) {
        //更改备注失败回调函数
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none'
        });
      }
    });
  },

  if_obj_is_null: function (obj) {
    let flag = false   // 判断一个对象下是否有空属性
    for (const key in obj) {
      if (obj.hasOwnProperty(key)) {
        if (obj[key] === null || obj[key] === '') {
          flag = true
        }
      }
    }
    return flag
  },

  getToBase64: function (file) {
    wx.getFileSystemManager().readFile({
      filePath: file.url, //选择图片返回的相对路径
      encoding: 'base64', //编码格式：base64 | hex(16进制)
      success: res => { //成功的回调
        let userImageBase64 = 'data:image/jpg;base64,' + res.data;
        let info = this.data.info
        info.photo = userImageBase64
        this.setData({ info })
      }
    })
  },
  uploadImg: function (e) {
    console.log(e, "-----")
    const { file } = e.detail;
    let _this = this
    let url = file[0].url

    wx.uploadFile({
      url: api.uploadPhoto,
      filePath: url,
      name: 'file',
      formData: { 
        appId: 'false',
        rename: 'true' ,
        uploadPath: 'member', 
      },
      success(res) {
        // 上传完成需要更新 fileList
        let info = _this.data.info
        info.photos = file
        info.photo = res.data && JSON.parse(res.data).data
        _this.setData({ info })
      }
    })
  },

  deleteImage: function (e) {
    let info = this.data.info
    info.photos = null
    info.photo = null
    this.setData({ info })
  }
});
